package cn.jy.map.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.log4j.Logger;

import cn.jy.map.domain.AddressMsg;
import cn.jy.map.domain.PlasmaResult;
import cn.jy.map.domain.PulpWeight;
import cn.jy.map.domain.VillageColligate;

public class MapUtils {
	
	private static Logger logger = Logger.getLogger(MapUtils.class);
	
	
	
	/**
	 * 护士基本工资配置
	 */
	private static Properties baseWagesProp = null;//
	/**
	 * 岗位绩效比例配置
	 */
	private static Properties postWagesProp = null;
	
	/**
	 * 采浆室护士集合
	 */
	private static final String[] COLLNURSELIST = {"mxz14;於孝楼","mxz19;张 君","mxz18;王爱弟","mxz43;黄亚琴","mxz42;孔德雅","mxz26;褚义霞","mxz46;王小艳","mxz47;陈丽阳"};
	/**
	 * 采浆室护士集合
	 */
	public static final Map<String,String> COLLNURSEMAP = new HashMap<String, String>();
	static{
		for (String nurse : COLLNURSELIST) {
			String[] split = nurse.split(";");
			COLLNURSEMAP.put(split[0], split[1]);
		}
		
	}
	
	
	/**
	 * 浆员类型的缓存，存放各种浆员类型的集合
	 */
	public static Map<GjzType,List<String>> allTypeGjzMap = new HashMap<GjzType, List<String>>();
	/**
	 * 所有浆员类型的地址定位信息集合 
	 */
	public static Map<String ,List<AddressMsg>> allGjzJwdMap = new HashMap<String ,List<AddressMsg>>();
	/**
	 * 所有村落下面的浆员卡号集合
	 */
	public static Map<String ,List<String>> allVillProvider = new HashMap<String ,List<String>>();
	/**
	 * 所有村落地址经纬度信息
	 */
	public static Map<String ,AddressMsg> allDzJwd = new HashMap<String ,AddressMsg>();
	/**
	 * 存储所有2014-10-01前建档但是后面一直没有来的人
	 */
	public static List<String> allForgeProviderNo = new ArrayList<String>();
	/**
	 * 所有废弃浆员的拼接的sql字符串 ，例如：'R000078','401377','401376' 。。。。
	 */
	public static String allForgeProviderNoStr = "";
	
	/**
	 * 存储所有村落的综合信息
	 */
	public static Map<String,VillageColligate> allVillageColligate = new HashMap<String ,VillageColligate>();
	public static List<VillageColligate> villList=new ArrayList<VillageColligate>();
	/**
	 * 存储今年与去年新增浆员
	 */
	public static Map<String, Object> thisLast=new HashMap<String,Object>();
	/**
	 * 存储今年与去年同期浆量新增浆员比
	 */
	public static Map<String, Object> thisLastPulp=new HashMap<String,Object>();
	
	
	public static Map<String, Object> jzAchievements=new HashMap<String,Object>();
	
	public static Map<String,PlasmaResult> resultMap = new HashMap<String, PlasmaResult>();
	
	public static long betweenMinutes(String beginDate, String endDate) throws Exception {
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date begin = dfs.parse(beginDate);
		Date end = dfs.parse(endDate);
		long between = (end.getTime() - begin.getTime()) / 1000;
		long min = between / 60;
		return min;
	}
	
	/**
	 * 获取当前年
	 * @return
	 */
	public static String getCurrentYear(){
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy");
		return dfs.format(new Date());
	}
	/**
	 * 获取当前月
	 * @return
	 */
	public static int getCurrentMonth(){
		SimpleDateFormat dfs = new SimpleDateFormat("MM");
		return Integer.parseInt(dfs.format(new Date()));
	}
	/**
	 * 获取当前小时
	 * @return
	 */
	public static int getCurrentHour(){
		SimpleDateFormat dfs = new SimpleDateFormat("HH");
		return Integer.parseInt(dfs.format(new Date()));
	}
	/**
	 * 获取指定日期的前或者后num年
	 * @return : yyyy-MM-dd
	 */
	public static String getThatDate(String date,int num){
		 Calendar calendar = Calendar.getInstance();
	        Date d = MapUtils.parseDate(date);
	        calendar.setTime(d);
	        calendar.add(Calendar.YEAR, num);
	        d = calendar.getTime();
	        return MapUtils.getNowOnlyDateFomat1(d);
	}
	/**
	 * 获取指定日期的前或者后num年/月/天，根据类型
	 * @param type : 类型：type = Calendar.YEAR、Calendar.MONTH、Calendar.DATE
	 * @return : yyyy-MM-dd
	 */
	public static String getThatDate(String date,int num,int type){
		Calendar calendar = Calendar.getInstance();
		Date d = MapUtils.parseDate(date);
		calendar.setTime(d);
		calendar.add(type, num);
		d = calendar.getTime();
		return MapUtils.getNowOnlyDateFomat1(d);
	}
	public static void main(String[] args) {
		System.out.println(getThatDate("2018-05-11", 1,1));
	}
	
	/**
	 * 获取指定时间的年份
	 * @param date 格式：yyyy-MM-dd HH:mm:ss 或者yyyy-MM-dd
	 * @return
	 */
	public static String getThatYear(String date){
		
		SimpleDateFormat format = null;
		
		if(date.length() > 10){
			format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		}else{
			format = new SimpleDateFormat("yyyy-MM-dd");
		}
		
		
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy");
		Date d = null; 
		try {
			d = format.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return dfs.format(d);
		
	}
	/**
	 * 获取指定年份的前N年或者后N年
	 * 如果num为 正数，则是获取后 num 年，如果为负数，则为前面num年
	 * @param year ： 格式：yyyy 
	 * @return
	 */
	public static String getThatYear(String year,int num){
		
		int y = Integer.parseInt(year);
		
		return (y + num) + "";
		
	}
	
	/**
	 * 格式化指定日期，格式：yyyy-MM-dd
	 * @return
	 */
	public static String getNowOnlyDateFomat1(Date date){
		String dfs = new SimpleDateFormat("yyyy-MM-dd").format(date);
		return dfs;
	}
	/**
	 * 获取当前日期，无分钟数，格式：yyyy-MM-dd
	 * @return
	 */
	public static String getNowOnlyDateFomat1(){
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd");
		return dfs.format(new Date());
	}
	/**
	 * 获取当前日期，无分钟数，格式：yyyyMMdd
	 * @return
	 */
	public static String getNowOnlyDateFomat2(){
		SimpleDateFormat dfs = new SimpleDateFormat("yyyyMMdd");
		return dfs.format(new Date());
	}
	/**
	 * 获取当前时间，格式：yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getNow(){
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return dfs.format(new Date());
	}
	/**
	 * 设置某个日期，转换格式为指定日期的指定时间
	 * @param date ：指定日期，格式：yyyy-MM-dd
	 * @param hour 指定小时，格式 ：0 -- 23 
	 * @param minute 指定分钟，格式：0 -- 59
	 * 
	 * 例如： getDateHour("2017-09-09","5","35")，则返回格式为："2017-09-09 05:35:00"
	 * 
	 * @return
	 */
	public static String getDateHour(String date,String hour,String minute){
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date d = dfs.parse(date + " "+ hour + ":" + minute + ":00");
			return dfs.format(d);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 格式化日期为 yyyyMMdd
	 * @param date 格式为：yyyy-MM-dd
	 * @return
	 */
	public static String forMatDate(String date){
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date d = dfs.parse(date);
			dfs = new SimpleDateFormat("yyyyMMdd");
			return dfs.format(d);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 将字符串转换为日期
	 * @param date 格式为：yyyy-MM-dd
	 * @return
	 */
	public static Date parseDate(String date){
		
		if(date != null && !"".equals(date)){
			if(date.length() > 10){
				date = date.substring(0, 10);
			}
			SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd");
			try {
				Date d = dfs.parse(date);
				return d;
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 由出生日期计算年龄
	 * @param birthDay
	 * @return
	 * @throws Exception
	 */
    public static int getAge(Date birthDay) {  
        Calendar cal = Calendar.getInstance();  
  
        if (cal.before(birthDay)) {  
            throw new IllegalArgumentException(  
                    "The birthDay is before Now.It's unbelievable!");  
        }  
        int yearNow = cal.get(Calendar.YEAR);  
        int monthNow = cal.get(Calendar.MONTH);  
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);  
        cal.setTime(birthDay);  
  
        int yearBirth = cal.get(Calendar.YEAR);  
        int monthBirth = cal.get(Calendar.MONTH);  
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);  
  
        int age = yearNow - yearBirth;  
  
        if (monthNow <= monthBirth) {  
            if (monthNow == monthBirth) {  
                if (dayOfMonthNow < dayOfMonthBirth) age--;  
            }else{  
                age--;  
            }  
        }  
        return age;  
    }
    
    /**
     * 获取去年的今天
     * @return
     */
    public static String getLastYearNow(){
    	SimpleDateFormat aSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        GregorianCalendar aGregorianCalendar = new GregorianCalendar();
        // Get last month GregorianCalendar object
        aGregorianCalendar.set(Calendar.YEAR, aGregorianCalendar
                .get(Calendar.YEAR) - 1);
        // Format the date to get year and month
        String lastYearNow = aSimpleDateFormat
                .format(aGregorianCalendar.getTime());
        
        return lastYearNow;
    }

    /**
     * 格式化时间格式，目标格式为：yyyy-MM-dd
     * @param date 格式为：yyyy-MM-dd HH:mm:ss
     * @return
     */
	public static String formatDate1(String date) {
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date d = dfs.parse(date);
			 dfs = new SimpleDateFormat("yyyy-MM-dd");
			 return dfs.format(d);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String formatDate(Date minBodyCheckTime) {
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return dfs.format(minBodyCheckTime);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	} 
	
	/**
	 * 获取两个集合的交集
	 */
	public static List<String> retainList(List<String> list1,List<String> list2){
		List<String> list = new ArrayList<String>(list1);
		
		list.retainAll(list2);
		
//		if(list1 != null && list1.size()>0 && list2 != null && list2.size()>0){
//			for(String var : list1){
//				for(String var1 : list2){
//					if(var.equals(var1)){
//						list.add(var);
//						break;
//					}
//				}
//			}
//		}
//		
		return list;
		
	}
	/**
	 * 获取两个集合的差集
	 */
	public static List<String> removeList(List<String> list1,List<String> list2){
		List<String> list = new ArrayList<String>(list1);
		
		list.removeAll(list2);
		
//		if(list1 != null && list1.size()>0 && list2 != null && list2.size()>0){
//			for(String var : list1){
//				for(String var1 : list2){
//					if(var.equals(var1)){
//						list.add(var);
//						break;
//					}
//				}
//			}
//		}
//		
		return list;
		
	}
	
    /** 
     * 获取某年某月的最后一天 
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(int year,int month)  
    {  
        Calendar cal = Calendar.getInstance();  
        //设置年份  
        cal.set(Calendar.YEAR,year);  
        //设置月份  
        cal.set(Calendar.MONTH, month-1);  
        //获取某月最大天数  
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);  
        //设置日历中月份的最大天数  
        cal.set(Calendar.DAY_OF_MONTH, lastDay);  
        //格式化日期  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
        String lastDayOfMonth = sdf.format(cal.getTime());  
          
        return lastDayOfMonth;  
    }
	
	
    /**
     * 获取护士岗位绩效比例
     * @param nurseId
     * @return
     */
    public static Map<String,Float> getNursePostWagesProportion(){
    	
    	//加载护士基本工资配置文件
    	if(postWagesProp == null){
    		postWagesProp = new Properties();
    		
 			
 			try {
 				postWagesProp.load(MapUtils.class.getResourceAsStream("postWagesProportion.properties"));  
 				
 			}catch (Exception e) {
    			logger.error("装载护士岗位绩效比例配置文件失败",e);
    		}
    	}
    	
    	 Map<String,Float> map = new HashMap<String,Float>();
    	
    	Set<Object> keys = postWagesProp.keySet();
    	
    	if(keys != null && keys.size()>0){
    		
    		for (Object obj : keys) {
    			String key = (String)obj;
    			String prop = postWagesProp.getProperty(key);
    			
    			Float postProp = Float.parseFloat(prop);
    			if(postProp != null){
    				map.put(key, postProp);
    			}
				
			}
    		
    	}
    	logger.info("所有有岗位绩效比例的护士的笔记集合：" + map);
    		
    	
    	return map;
    }
	/**
	 * 获取护士的基本工资
	 * @param nurseId
	 * @return
	 */
	public static float getNurseBaseWages(String nurseId){
		
		//加载护士基本工资配置文件
		if(baseWagesProp == null){
			baseWagesProp = new Properties();
			
			try {
				baseWagesProp.load(MapUtils.class.getResourceAsStream("nurseBaseWages.properties"));  
				
			} catch (Exception e) {
				logger.error("装载护士基本工资配置文件失败",e);
			}
			
		}
		float baseWage = -1f;
		
		if(nurseId != null){
			
			
			String property = baseWagesProp.getProperty(nurseId);
			if(property != null && !"".equals(property)){
				try {
					baseWage = Float.parseFloat(property);
					logger.info("获取护士【"+nurseId+"】的基本工资为【"+baseWage+"】");
				} catch (Exception e) {
					logger.error("转换配置文件中护士基本工资为Float失败",e);
				}
			}
		}
		
		return baseWage;
	}

	 public static List<Date> findDates(Date dBegin, Date dEnd) {
         List<Date> lDate = new ArrayList<Date>();
         lDate.add(dBegin);
         Calendar calBegin = Calendar.getInstance();
     // 使用给定的 Date 设置此 Calendar 的时间
         calBegin.setTime(dBegin);
         Calendar calEnd = Calendar.getInstance();
     // 使用给定的 Date 设置此 Calendar 的时间
           calEnd.setTime(dEnd);
         // 测试此日期是否在指定日期之后
         while (dEnd.after(calBegin.getTime()))  {
          // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
              calBegin.add(Calendar.MONTH, 1);
              lDate.add(calBegin.getTime());
         }
         return lDate;
    }
	
}
